package com.project.dao.impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
 * 用户持久实现
 */
@Repository
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean land(String name, String password) {
        this.setConnection();
        UserBean user = null;
        try {
            ps = con.prepareStatement("SELECT * FROM t_user WHERE t_name=? AND t_password = ?");
            ps.setObject(1,name);
            ps.setObject(2,password);
            rs = ps.executeQuery();
            while (rs.next()){
                user = new UserBean();
                user.setId(rs.getInt("pk_userId"));
                user.setUserName(rs.getString("t_name"));
                user.setPassword(rs.getString("t_password"));
                user.setUserMail(rs.getString("t_userMail"));
                user.setUserImg(rs.getString("t_userImg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return user;
    }

    @Override
    public void add(UserBean user) {
      this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_user(t_name,t_password,t_userMail,t_userImg) values (?,?,?,?)");
            ps.setObject(1,user.getUserName());
            ps.setObject(2,user.getPassword());
            ps.setObject(3,user.getUserMail());
            ps.setObject(4,user.getUserImg());
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.setConnection();
        }
    }

}
